
<template>
  <div id="hotSetting">
    <el-dialog width="790px" :visible.sync="hotShown" title="热区编辑器">
      <div class="tip">
        <div class="tip-content">
          <span>1</span>
          <span>添加热区</span>
          <span></span>
        </div>
        <svg style="font-size:20px" class="icon" aria-hidden="true">
          <use xlink:href="#iconjiantou1" />
        </svg>
        <div class="tip-content">
          <span>2</span>
          <span>调整热区大小和位置</span>
          <span></span>
        </div>
        <svg style="font-size:20px" class="icon" aria-hidden="true">
          <use xlink:href="#iconjiantou1" />
        </svg>
        <div class="tip-content">
          <span>3</span>
          <span>设置关联链接</span>
          <span></span>
        </div>
        <svg style="font-size:20px" class="icon" aria-hidden="true">
          <use xlink:href="#iconjiantou1" />
        </svg>
        <div class="tip-content">
          <span>4</span>
          <span>保存生效</span>
          <span></span>
        </div>
      </div>

      <div class="drawHotAreaContent">
        <img class="drawHotAreaBg" draggable="false" :src="styles.img" />
        <div
          v-show="v.status"
          v-for="(v,i) in divList"
          :key="i"
          class="react-drag"
          :style="{
              width:v.width+'px',
              height: v.height+'px',
              left: '10px',
              top: '10px',
              transform: 'translateX('+v.sX+'px) translateY('+v.sY+'px)'}"
        >
          <span @click="openUrl(i)" :num='JSON.stringify(v.url)'>{{v.url==''?'添加链接':JSON.parse(v.url).name.name}}</span>
          <div @click="deleted(i)" class="drag-close">x</div>
        </div>
      </div>
      <div class="btn">
        <el-button @click="saveHot" size="small" plain>保存</el-button>
        <el-button @click="addDiv" size="small" type="primary">添加热区</el-button>
      </div>
    </el-dialog>
    <urlSelect ref="urlSelect" @getUrl='getUrl'></urlSelect>
  </div>
</template>

<script>
import $ from "jquery";
import draw from "../../../assets/js/drawHotArea";
import urlSelect from "../../comm/urlSelect";
export default {
  name: "hotSetting",
  data() {
    return {
      hotShown: false,
      divList: [{ width: 200, height: 200, sX: 0, sY: 0, status: true,url:[] }],
      currentDivId:"",
    };
  },
  props: ["styles"],
  components: {
    urlSelect,
  },
  created() {},
  methods: {
    getUrl(url){
      console.log(url)
      this.divList[this.currentDivId].url = JSON.stringify(url)
    },
    init() {
      draw();
      console.log(this.styles.divStyles);
      if (this.styles.divStyles.length != 0) {
        var list = JSON.stringify(this.styles.divStyles);
        this.divList = JSON.parse(list);
      }
    },
    deleted(i) {
      this.divList[i].status = false;
    },
    openUrl(i) {
      this.$refs["urlSelect"].cateShown = [true, true, true, true, true];
      this.$refs["urlSelect"].isShown = true;
      this.$refs["urlSelect"].getMarketPage()
      this.currentDivId = i
    },
    addDiv() {
      this.divList.push({
        width: 200,
        height: 200,
        sX: 0,
        sY: 0,
        status: true,
        url:""
      });
      draw();
    },
    saveHot() {
      let newDivList = [];
      let list = this.divList;
      $(".drawHotAreaContent .react-drag").each(function (i, dom) {
        var divNum = $(this);
        if (list[i].status) {
          var arr = {
            width: divNum.css("width").replace("px", ""),
            height: divNum.css("height").replace("px", ""),
            sX: divNum.css("transform").substring(7).split(",")[4],
            sY: divNum
              .css("transform")
              .substring(7)
              .split(",")[5]
              .replace(")", ""),
            status: true,
            url:JSON.parse(divNum.children('span').attr('num')),
            name:divNum.children('span').text()
          };
          newDivList.push(arr);
        }
      });
      console.log(newDivList)
      this.styles.divStyles = newDivList;
      this.hotShown = false;
    },
  },
};
</script>

<style lang="less" scoped>
.drawHotAreaContent {
  width: 100%;
  height: auto;
  position: relative;
  transform: translate3d(0, 0, 0);
  overflow-x: hidden;
  overflow-y: hidden;
}
.drawHotAreaBg {
  width: 100%;
  height: auto;
  vertical-align: middle;
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
}
.react-drag {
  border: 1px solid #38f;
  background: rgba(51, 136, 255, 0.5);
  color: #fff;
  position: absolute;
  cursor: move;
  user-select: auto;
  -webkit-user-select: auto;
  -moz-user-select: auto;
  display: flex;
  justify-content: center;
  align-items: center;
  > span {
    cursor: pointer;
  }
}
.react-drag:hover {
  .drag-close {
    display: block;
  }
}
.drag-close {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.5);
  position: absolute;
  right: -8px;
  top: -8px;
  text-align: center;
  font-size: 8px;
  line-height: 12px;
  display: none;
  cursor: pointer;
}
.btn {
  margin-top: 20px;
  text-align: center;
}
.tip {
  display: flex;
  justify-content: space-around;
  align-items: center;
  margin-bottom: 20px;
  .tip-content {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    color: #999;
    > span:nth-of-type(1) {
      display: inline-block;
      border: 1px solid #eee;
      width: 30px;
      height: 30px;
      border-radius: 50%;
      text-align: center;
      line-height: 30px;
      margin-right: 10px;
    }
  }
}
</style>
